//
//  MPTLiveMeHeadView.m
//  MPTLive
//
//  Created by caixun on 16/1/27.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "MPTLiveMeHeadView.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define ImageScale (166/221.0)
// 最后减的20是:图片和图片之间间距是10，三张图片两个间距，所以为20
#define ImgvW ((((screenW-30)-20)/3))


@interface MPTLiveMeHeadView ()


// 用户头像
@property (nonatomic, strong) UIImageView *imgvUserHead;
// 用户名称
@property (nonatomic, strong) UILabel *labelUserName;
// 用户信息描述
@property (nonatomic, strong) UILabel *labelUserDesc;
// 承载用户头像下面的三张图片的view
@property (nonatomic, strong) UIView *viewIcon;
// 右侧箭头
@property (nonatomic, strong) UIImageView *imgvJianTou;
//
@property (nonatomic, strong) UIButton *btnBg;
//
@property (nonatomic, strong) UIView *viewLine;


@end


@implementation MPTLiveMeHeadView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initContentView];
    }
    
    return self;
}

- (void)initContentView
{
    self.backgroundColor = [UIColor whiteColor];
    // 创建 用户头像
    self.imgvUserHead = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 69, 69)];
    [self addSubview:self.imgvUserHead];
    
    // 创建 右侧箭头
    self.imgvJianTou = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-15-8,
                                                                     0,
                                                                     8,
                                                                     14)];
    self.imgvJianTou.image =[UIImage imageNamed:@"JianTou"];
    self.imgvJianTou.center = CGPointMake(self.imgvJianTou.center.x, self.imgvUserHead.center.y);
    [self addSubview:self.imgvJianTou];
    
    // 创建 用户名称
    CGRect rect = CGRectMake(CGRectGetMaxX(self.imgvUserHead.frame)+10,
                             0,
                             self.frame.size.width-(CGRectGetMaxX(self.imgvUserHead.frame)+10),
                             20);
    self.labelUserName = [[UILabel alloc] initWithFrame:rect];
    [self.labelUserName setTextAlignment:NSTextAlignmentLeft];
    self.labelUserName.font = [UIFont systemFontOfSize:16.0f];
    self.labelUserName.textColor = RGBAColor(40, 40, 40, 1.0);
    [self addSubview:self.labelUserName];
    self.labelUserName.center = CGPointMake(self.labelUserName.center.x,
                                            self.imgvUserHead.center.y);
    
    // 创建 用户信息描述
    CGRect rect1 = CGRectMake(self.labelUserName.frame.origin.x,
                              CGRectGetMaxY(self.labelUserName.frame) + 5,
                              self.labelUserName.frame.size.width,
                              self.labelUserName.frame.size.height);
    self.labelUserDesc = [[UILabel alloc] initWithFrame:rect1];
    [self.labelUserDesc setTextAlignment:NSTextAlignmentLeft];
    self.labelUserDesc.font = [UIFont systemFontOfSize:13.0f];
    self.labelUserDesc.textColor = RGBAllColor(0x666666);
    [self addSubview:self.labelUserDesc];
}


/// 使用数据
- (void)setDicData:(NSDictionary *)dicData
{
    _dicData = dicData;
    /// 头像
    [_imgvUserHead sd_setImageWithURL:[NSURL URLWithString:dicData[@"header"][@"icon"]] placeholderImage:nil];
    /// 名称
    _labelUserName.text = dicData[@"header"][@"nick"];
    
    /// 直播次数被赞次数
    _labelUserDesc.text = [NSString stringWithFormat:@"直播%@次 / 被赞%@次",
                           dicData[@"header"][@"liveCnt"][@"total"]
                           ,dicData[@"header"][@"liveCnt"][@"like"]];
    
    
    // 创建
    CGRect rect3 = CGRectMake(0, 0, self.frame.size.width, 0);
    
    if ([dicData[@"result"] isKindOfClass:[NSArray class]])
    {
        if([dicData[@"result"] count] > 0)
        {
            
            // 创建 承载用户头像下面的三张图片的view
            CGRect rect2 = CGRectMake(self.imgvUserHead.frame.origin.x,
                                      CGRectGetMaxY(self.imgvUserHead.frame)+15,
                                      self.frame.size.width-30,
                                      ImageScale*ImgvW);
            self.viewIcon = [[UIView alloc] initWithFrame:rect2];
            _viewIcon.clipsToBounds = NO;
            [self addSubview:self.viewIcon];
            
            for (int i = 0; i < [dicData[@"result"] count] ; i++)
            {
                if (i == 3)
                {
                    break;
                }
                
                if ([dicData[@"result"][i][@"channel"] isKindOfClass:[NSDictionary class]])
                {
                    if ([dicData[@"result"][i][@"channel"][@"pic"] isKindOfClass:[NSDictionary class]])
                    {
                        CGRect rect = CGRectMake(i*ImgvW+i*10,
                                                 0,
                                                 ImgvW,
                                                 ImageScale*ImgvW);
                        UIImageView *imgv = [[UIImageView alloc] initWithFrame:rect];
                        imgv.contentMode = UIViewContentModeScaleAspectFill;
                        imgv.clipsToBounds = YES;
                        [self.viewIcon addSubview:imgv];

                        NSDictionary *dic = dicData[@"result"][i][@"channel"][@"pic"];
                        NSString *str = [NSString stringWithFormat:@"%@%@",dic[@"base"],dic[@"m"]];
                        [imgv sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil];
                    }
                }

            }
            /// 线
            self.viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewIcon.frame)+14.5, screenW, 0.5)];
            self.viewLine.backgroundColor = RGBAllColor(0xdddddd);
            [self addSubview:self.viewLine];
        }
        else
        {
            /// 线
            self.viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imgvUserHead.frame)+15, screenW, 0.5)];
            self.viewLine.backgroundColor = RGBAllColor(0xdddddd);
            [self addSubview:self.viewLine];
        }
    }
    /// 点击事件
    self.btnBg = [UIButton buttonWithType:UIButtonTypeSystem];
    self.btnBg.frame = CGRectMake(0, 0, screenW, CGRectGetMaxY(self.viewLine.frame));
    [self addSubview:self.btnBg];
}


+ (float)getViewH:(NSDictionary *)dic
{
    if([dic[@"result"] count] > 0)
    {
        return 99 + ImageScale*ImgvW +15;
    }
    else
    {
        return 99;
    }
}

- (void)setBtnTarget:(id)target withShowInfoAction:(SEL)infoAction
{
    [self.btnBg addTarget:target action:infoAction forControlEvents:UIControlEventTouchUpInside];
}


@end
